package com.example.promethues.strategy;

import com.example.promethues.dto.PromApiData;
import com.example.promethues.dto.PromVectorDataResult;
import com.example.promethues.vo.DatasetVo;
import com.example.promethues.vo.TSVo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PromScalarConvertStrategy extends PromConvertStrategy {
    public PromScalarConvertStrategy(String resultType) {
        super(resultType);
    }

    @Override
    public List<DatasetVo> toDataSets(PromApiData promApiData) {
        List<DatasetVo> datasetVos = new ArrayList<>();
        Gson gson = new Gson();
        JsonArray resultArr = gson.toJsonTree(promApiData.getResult()).getAsJsonArray();
        List<String> promScalarDataResults = gson.fromJson(resultArr, new TypeToken<List<String>>() {
        }.getType());

        List<TSVo> tsVos = new ArrayList<>();

        String value0 = new BigDecimal(promScalarDataResults.get(0)).toPlainString();
        tsVos.add(new TSVo(Long.parseLong(value0.substring(0, value0.indexOf("."))) * 1000, promScalarDataResults.get(1)));

        DatasetVo datasetVo = new DatasetVo();
        datasetVo.setSource(tsVos);
        datasetVos.add(datasetVo);

        return datasetVos;
    }
}
